package com.xuecheng.learning.service.impl;

import com.xuecheng.api.client.ContentClient;
import com.xuecheng.api.client.MediaClient;
import com.xuecheng.api.dto.CoursePublish;
import com.xuecheng.api.dto.MediaFiles;
import com.xuecheng.api.dto.TeachPlanAndMediaDTO;
import com.xuecheng.base.constant.CourseChargeConstant;
import com.xuecheng.base.constant.CoursePreviewConstant;
import com.xuecheng.base.constant.LearningQualificationConstant;
import com.xuecheng.base.exception.XCPlusBaseException;
import com.xuecheng.base.util.SecurityUtil;
import com.xuecheng.learning.exception.CourseChargeHasBeenExpiredException;
import com.xuecheng.learning.exception.CourseTeachPlanChargeException;
import com.xuecheng.learning.exception.LearningError;
import com.xuecheng.learning.model.dto.XcCourseTablesDTO;
import com.xuecheng.learning.service.IOpenLearningService;
import com.xuecheng.learning.service.IXcCourseTablesService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @author 勾新杰
 * @version 1.0
 * @description:
 * @date 2025/3/11 17:35
 */
@Service
@RequiredArgsConstructor
public class IOpenLearningServiceImpl implements IOpenLearningService {

    private final ContentClient contentClient;

    private final MediaClient mediaClient;

    private final IXcCourseTablesService xcCourseTablesService;

    /**
     * 获取视频
     *
     * @param courseId    课程id
     * @param teachplanId 课程计划id
     * @param mediaId     视频id
     * @return 视频播放地址
     */
    @Override
    public String getVideo(Long courseId, Long teachplanId, String mediaId) {
        // 1. 获取课程计划、绑定的媒资信息、课程发布信息
        TeachPlanAndMediaDTO teachPlanAndMedia = contentClient.getTeachPlanAndMedia(teachplanId);
        CoursePublish coursePublish = contentClient.getById(courseId);
        // 2. 获取该课程是否是收费课程
        String charge = coursePublish.getCharge();
        // 3. 进行参数合法性校验
        if (teachPlanAndMedia == null
                || !mediaId.equals(teachPlanAndMedia.getMediaId())
                || !courseId.equals(teachPlanAndMedia.getCourseId())) {
            throw new XCPlusBaseException(LearningError.ILLEGAL_ARGUMENT.getErrMsg());
        }
        // 4. 如果课程是免费课程，直接返回视频播放地址
        if (CourseChargeConstant.FREE.equals(charge))
            return mediaClient.queryMediaById(mediaId).getUrl();
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        // 5. 如果用户已登录，判断用户是否可以学习该课程
        if (user != null) {
            // 5.1 根据课程id和用户获取用户学习资格
            XcCourseTablesDTO qualification = xcCourseTablesService.getLearningQualification(courseId, user.getId());
            String learnStatus = qualification.getLearnStatus();
            // 5.2 根据media id获取媒资信息
            MediaFiles mediaFiles = mediaClient.queryMediaById(mediaId);
            // 5.3 如果用户学习资格为正常学习，则返回视频播放地址
            if (LearningQualificationConstant.NORMAL_LEARNING.equals(learnStatus)) {
                return mediaFiles.getUrl();
            }
            // 5.4 如果用户学习资格为未选课或未支付，则判断是否是免费课程，如果是免费课程则返回视频播放地址，否则抛异常
            else if (LearningQualificationConstant.NOT_CHOOSE_OR_NOT_PAY.equals(learnStatus)) {
                if (CoursePreviewConstant.IS_PREVIEW.equals(teachPlanAndMedia.getIsPreview()))
                    return mediaFiles.getUrl();
                else throw new CourseTeachPlanChargeException();
            }
            // 5.5 如果用户学习资格为已过期，则判断是否是免费课程，如果是免费课程则返回视频播放地址，否则抛异常
            else {
                if (CoursePreviewConstant.IS_PREVIEW.equals(teachPlanAndMedia.getIsPreview()))
                    return mediaFiles.getUrl();
                else throw new CourseChargeHasBeenExpiredException();
            }
        }
        // 6. 如果用户未登录，判断是否是免费课程，如果是免费课程则直接返回视频播放地址，否则抛异常
        else {
            if (CoursePreviewConstant.IS_PREVIEW.equals(teachPlanAndMedia.getIsPreview()))
                return mediaClient.queryMediaById(mediaId).getUrl();
            else throw new CourseTeachPlanChargeException();
        }
    }
}
